export default {
  data() {
    return {
      pageInfo: {
        currentPage: 1,
        pageSizes: [20, 50, 100, 200],
        pageSize: 20,
        total: 0,
      },
      filter: {},
      tableData: [],
      isSelectAll: [false],
      selectAllData: []
    }
  },
  mounted() {

    this.getfilter()
  },
  methods: {
    _search(sign) {

      if (sign === 'search') {
        this.getfilter()
        this.pageInfo.currentPage = 1
      }
      let params = Object.assign({
        page: this.pageInfo.currentPage,
        pageSize: this.pageInfo.pageSize,
        // userState:1
      }, this.filter);
      this.$http(`${this.url}`, params).then(res => {
        this.tableData = res.data.list;
        // this.sessionSet('roleType',res.data.roleList)
        this.pageInfo.total = res.data.total;
        this.$emit('dataChange', res.data)
        // setTimeout(() => {

        // }, timeout);
        //
      });

    },
    handleSizeChange(pageSize) {
      //
      this.pageInfo.pageSize = pageSize;
      this._search()
      this.$emit('sizeChange');
    },

    handleCurrentChange(page) {
      this.pageInfo.currentPage = page;
      this._search()
      this.$emit('pageChange');
    },
    getfilter() {
      let filter = {}
      for (let key in this.where) {
        filter[key] = this.where[key].value
      }
      this.filter = filter
    },
    signSelect() {
      let page = this.pageInfo.currentPage;
      let rows = this.selectData[page]
      //
      // this.$refs.multipleTable.toggleRowSelection(this.tableData[1],true);
      if (rows) {

        rows.forEach(row => {
          let i = this._gitIndex(row)
          //
          i !== null ? this.$refs.vTable.toggleRowSelection(this.tableData[i]) : '';
        });
      } else {
        this.$refs.vTable.clearSelection();
      }
    },
    _gitIndex(row) {
      let i = null
      this.tableData.some((item, index) => {
        // if (JSON.stringify(row) === JSON.stringify(item)) {
        //   i = index
        //   return true
        // }
        if (item[this.uniqueKey] == row[this.uniqueKey]) {
          i = index
          return true
        }
      })
      return i;
    },
    _select(selection, row) {
      //
      this.addSelectData(row);
      this.$emit('selectChange', this.selectData);
    },
    _selectAll(selections, row) {
      let page = this.pageInfo.currentPage;
      this.selectData[page] = JSON.parse(JSON.stringify(selections))
      console.log('%c gc_ this.selectData[page]=>: ','color: MidnightBlue; background: Aquamarine; font-size: 20px;', this.selectData[page]);
      this.$emit('selectChange', this.selectData);
    },
    addSelectData(row) {
      let flag = true;
      let page = this.pageInfo.currentPage
      //当前页没有选中数据时
      if (!this.selectData[page]) {
        this.selectData[page] = [row];
        //
        return
      }
      // console.log('%c gc_this.selectData[page]=>: ','color: MidnightBlue; background: Aquamarine; font-size: 20px;',this.selectData[page]);

      this.selectData[page].some((item, i) => {
        console.log('%c gc_item.id=>: ','color: MidnightBlue; background: Aquamarine; font-size: 20px;',item,row);
        if (item[this.uniqueKey] == row[this.uniqueKey]) {
          console.log('闪到了')
          this.selectData[page].splice(i, 1);
          flag = false;
          return true;
        }
      });
      flag ? this.selectData[page].push(row) : '';
      //
    },
  },
  watch: {
    'tableData'(val) {
      if (this.select) {
        setTimeout(() => {
          this.signSelect()
        }, 0);
      }
    },
    'selectData'(val) {
      //
    }
  }
}